package com.example.video.file_server.controller;

import com.example.video.common.util.ResponseWrap;
import com.example.video.common.util.ResponseWrapUtil;
import com.example.video.file_server.common.FFmpeg.domain.VideoMetaInfo;
import com.example.video.file_server.entity.FileShardInfo;
import com.example.video.file_server.service.IFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 文件 前端控制器
 * <p/>
 *
 * @author : yj
 */
@RestController
@RequestMapping("file")
public class FileController {

    @Autowired
    private IFileService iFileService;

    private static Map<Integer,InputStream> map = new HashMap();

    @PostMapping("upload")
    @ApiOperation("图片文件上传")
    public ResponseWrap upload(@RequestParam("file") MultipartFile file){
        String url = iFileService.upload(file);
        return ResponseWrapUtil.success(url,"success");
    }

    @PostMapping("delete")
    @ApiOperation("删除文件")
    public ResponseWrap delete(String bucketName, String objectName){
        iFileService.delete(bucketName, objectName);
        return ResponseWrapUtil.success("success");
    }

    @PostMapping("fragmentUploadUrls")
    @ApiOperation("获取视频文件上传分片信息")
    public ResponseWrap fragmentUpload(@RequestParam("file") MultipartFile file){
        Map<String,List<FileShardInfo>> urls = iFileService.fragmentUploadUrls(file);
        return ResponseWrapUtil.success(urls);
    }

    @PostMapping("mergeAndTranscode")
    @ApiOperation("视频合并转码m3u8")
    public ResponseWrap mergeAndTranscode(String objectName, String type){
        VideoMetaInfo videoMetaInfo = iFileService.mergeAndTranscode(objectName, type);
        return ResponseWrapUtil.success(videoMetaInfo);
    }

    @PostMapping("uploadByM3u8Url")
    @ApiOperation("通过m3u8视频文件路径下载视频后上传")
    public ResponseWrap<VideoMetaInfo> uploadByM3u8Url(String mau8Url){
        VideoMetaInfo videoMetaInfo = iFileService.uploadByM3u8Url(mau8Url);
        return ResponseWrapUtil.success(videoMetaInfo);
    }
}
